package com.glory.brepda.activity.lanjian;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.brepda.R;
import com.glory.brepda.adapter.LotSubmissionhisAdapter;
import com.glory.brepda.adapter.SFCStockingAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.SFCStockingEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class BreSFCStockingOutActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvLotQTY)
    TextView tvLotQTY;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    private SFCStockingAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sfcstocking_in;
    }

    @Override
    protected void initView() {
        tvBaseSubmit.setVisibility(View.GONE);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        initRecyclerView(rvLotList, this);
        mAdapter = new SFCStockingAdapter(null);
        mAdapter.openLoadAnimation(LotSubmissionhisAdapter.ALPHAIN);
        mAdapter.isFirstOnly(false);
        rvLotList.setAdapter(mAdapter);

        initEdit(etLot, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(BreSFCStockingOutActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    tvLotQTY.setText(response.getLOT().getMAINQTY());
                    tvLotState.setText(response.getLOT().getSTATE());
                    tvStepDesc.setText(response.getLOT().getSTEPDESC());
                    submit();
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });
        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
        }
    }

    private void submit(){

        if(getText(etLot).equals("")){
            showToast("请先扫描批次信息", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<SFCStockingEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SFCStockingEntity>>
                (new TypeToken<BaseResponseBean<SFCStockingEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<SFCStockingEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    showToast("出库成功", true);
                    mAdapter.addData(0, response.body().getResponse().getBody().getResult());

                    clean();
                    etLot.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<SFCStockingEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDA_WIP_SFC_STOCKING");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "OUT");
        map.addProperty("LOT_ID", getText(etLot));
        NetUtils.requestPostNet("IF.PDA_WIP_SFC_STOCKING", "Common", BreSFCStockingOutActivity.this, map, callBack);
    }

    public void clean(){
        etLot.setText("");
    }

    @Override
    protected void initData() {

    }

}